Walkthrough 10-2: Handle errors at the application level

In this walkthrough, you create a default error handler for the application. You will:

·       Create a global error handler in an application.

·       Configure an application to use a global default error handler.

·       Explore the differences between the On Error Continue and On Error Propagate scopes.

·       Modify the default error response settings for an HTTP Listener.

Starting file

If you did not complete the previous walkthrough, you can get a starting file here. This file is also located in the solutions folder of the student files ZIP located in the Course Resources.

Browse the error handling elements in the Mule Palette

1.     Return to global.xml and switch to the Message Flow view.

2.     In the Core section of the Mule Palette, locate the Error Handling elements.

 

Create a global error handler with an On Error Continue scope

3.     Drag out an Error Handler element and drop it in the canvas of global.xml.

4.     Drag out an On Error Continue element and drop it in globalError_Handler.

 

5.     In the On Error Continue properties view, click the Search button for type.

6.     In the drop-down menu that appears, select ANY.

 

Set the payload in the error handler to a JSON message

7.     Add a Transform Message component to the On Error Continue.

8.     Set the Transform Message display name to error.description.

9.     In the Transform Message properties view, change the output type to application/json.

10.  Add a message property to the output JSON and give it a value of the error.description.

Set a default error handler for the application

11.  Switch to the Global Elements view of global.xml.

12.  Click Create.

13.  In the Choose Global Type dialog box, select Global Configurations > Configuration and click OK.

 

14.  In the Global Element Properties dialog box, set the default error handler to globalError_Handler and click OK.

 

15.  Switch to the Message Flow view.

Review the default response settings for an HTTP Listener

16.  Return to implementation.xml.

17.  Navigate to the properties view for the GET /flights Listener in getFlights.

18.  Select the Responses tab.

19.  Locate the Response section (not the Error Response section) and review the default settings for the body, status code, and reason phrase.

  

20.  Locate the Error Response section and review the default settings for the body, status code, and reason phrase.

Test the On Error Continue behavior

21.  Save all files, debug the project, and proceed through any errors in the workspace.

22.  In Advanced REST Client, make another request to http://localhost:8081/flights?airline=american&code=PDX.

23.  In the Mule Debugger, step through the application until the event is passed to globalError_Handler.

24.  Expand the error object and review the exception.

25.  Step again; the event should be passed back to getFlights, which continues to execute after the error occurs.

26.  Review the payload and note the absence of an error object.

27.  Resume through the rest of the application.

28.  Return to Advanced REST Client; you should see get a 200 OK response with the response body equal to the payload value set in the error handler.

 

Change the default error handler to use an On Error Propagate scope

29.  Return to Anypoint Studio and switch to the Mule Design perspective.

30.  Return to global.xml.

31.  Drag an On Error Propagate element from the Mule Palette and drop it to the right or left of the On Error Continue to add it to globalError_Handler.

32.  In the On Error Propagate properties view, set the type to ANY.

33.  Move the Transform Message component from the On Error Continue to the On Error Propagate.

34.  Delete the On Error Continue scope.

Test the On Error Propagate behavior

35.  Save the files to redeploy the project.

36.  In Advanced REST Client, make another request to http://localhost:8081/flights?airline=american&code=PDX.

37.  Step through the application until the event is passed to globalError_Handler.

38.  Look at the payload and the exception in the Mule Debugger; they should be the same as before.

39.  Step again; the error is propagated up to the getFlights parent flow.

40.  Look at the payload and the error object in the Mule Debugger.

41.  Step twice more; the error is handled by the application’s default error handler again.

42.  Look at the payload and the exception in the Mule Debugger.

43.  Resume through the rest of the application.

44.  Return to Advanced REST Client; you should see get a 500 Server Error response with the response body equal to the plain text error description – not the payload.

 

45.  Return to Anypoint Studio and switch to the Mule Design perspective.

46.  Stop the project.

Modify the default error response settings for the HTTP Listener

47.  Return to implementation.xml.

48.  Navigate to the Responses tab in the properties view for the GET /flights Listener.

49.  Change the error response body to return the payload instead of error.description.

payload

 

Test the application

50.  Save the file, run the project, and proceed through any errors in the workspace.

51.  In Advanced REST Client, make another request to  http://localhost:8081/flights?airline=american&code=PDX; you should now get the message that you set in the error handler.

 

Note: You will handle this error differently in a later walkthrough, so it does not return a server error. It is a valid request; there are just no American flights to PDX.

52.  Change the airline to make a request to http://localhost:8081/flights?airline=delta&code=PDX; the error is handled by the same default handler.

 

53.  Return to Anypoint Studio.



Did you complete the walkthrough?

  Yes, I completed the walkthrough

  No, I did not complete the walkthrough

  I completed part of the walkthrough


Comments and/or feedback